<?php defined('MINICAKE') or exit('can not access!');
/**
 * Controller 
 * 
 * @uses Object
 * @package 
 * @version 1 
 * @copyright 
 * @author lovise <lovise.lu@gmail.com> 
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class Controller extends Object{
	/**
	 * name 
	 * 控制器的名字,用于自动加载同名的模型和视图.
	 * @var mixed
	 * @access public
	 */
	var $name = null;
	/**
	 * uses 
	 * 定义使用的模型 
	 * @var mixed
	 * @access public
	 */
	var $uses = array();
	/**
	 * layout 
	 * 使用的布局
	 * @var string
	 * @access public
	 */
	var $layout = 'default';
	/**
	 * autoLayout 
	 * 是否自动渲染布局
	 * @var mixed
	 * @access public
	 */
	var $autoLayout = true;
	/**
	 * pageTitle 
	 * 设置页面的标题,为了做到这一点，你需要在页面布局中把$title_for_layout放在HTML文档头部的标签之间。
	 * @var string
	 * @access public
	 */
	var $pageTitle = 'null';
	/**
	 * view 
	 * 定义使用的视图.默认与控制器同名
	 * @var mixed
	 * @access public
	 */
	var $view = null;
	/**
	 * autoRender 
	 * 是否自动渲染视图
	 * @var mixed
	 * @access public
	 */
	var $autoRender = true;
	/**
	 * view_vars 
	 * 视图中使用的变量
	 * @var array
	 * @access public
	 */
	var $viewVars = array();
	/**
	 * __construct 
	 * 构造函数
	 * @access public
	 * @return void
	 */
	function __construct(){
		parent::__construct();
		if(preg_match('/(.*)Controller/i',get_class($this),$this->name)){
			$this->name = $this->name[1];
		}else{
			throw new Exception('Controller::__construct() : Can not parse this class name."');
		}
		if($this->uses !== false){
			if(empty($this->uses)){
				$this->uses[] = $this->name;
			}
			foreach($this->uses as $model){
				$this->loadModel($model);
			}
		};
	}
	/**
	 * 赋值变量到模板
	 *
	 * @param unknown_type $key
	 * @param unknown_type $val
	 */
	function set($one,$two = null){
		$data = array();
		if (is_array($one)) {
			if (is_array($two)) {
				$data = array_combine($one, $two);
			} else {
				$data = $one;
			}
		} else {
			$data = array($one => $two);
		}
		$this->viewVars = $data + $this->viewVars;
	}
	/**
	 * render 
	 * 渲染页面(不输出)
	 * @param mixed $view 
	 * @access public
	 * @return void
	 */
	function render($view){
		extract($this->viewVars);
		$title_for_layout = $this->pageTitle;
		$charset_for_layout = Configure::getItem('charset');
		ob_start();
		if(file_exists(APP."views".DS."{$this->name}".DS."{$view}.ctp")){
			include_once(APP."views".DS."{$this->name}".DS."{$view}.ctp");
		}else{
			throw new Exception("Controller::render() : view {$view}.ctp not exists");
		}
		$content_for_layout = ob_get_clean();
		if($this->autoLayout === true){
			if(file_exists(APP."views".DS."layout".DS."{$this->layout}.ctp")){
			include_once(APP."views".DS."layout".DS."{$this->layout}.ctp");
			$output = ob_get_contents();
			}else{
				throw new Exception("Controller::render() : layout {$view}.ctp not exists");
			}
		}else{
			$output = $content_for_layout;
		}
		ob_end_clean();
		return $output;
	}
	/**
	 * 加载模型
	 *
	 * @param unknown_type $model
	 */
	function loadModel($model){
		if(file_exists(APP.'models/'.$model.'.php')){
			include_once(APP.'models/'.$model.'.php');
			if(class_exists($model)){
				$this->{$model} = new $model();
			}else{
				throw new Exception('Controller::loadModel() class $model not exists');
			}
		}else{
			throw new Exception("Controller::loadModel() {$model}.php not exists");
		}
	}

	/**
	 * beforeAction 
	 * 执行action前运行的函数
	 * @access public
	 * @return void
	 */
	function beforeAction(){
	}
}
?>
